package com.ctsi.ntedu.web;

import java.net.URI;
import java.net.URISyntaxException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Optional;
import java.lang.Integer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ctsi.ntedu.service.NteduTelService;
import com.ctsi.ntedu.domain.NteduTel;
import com.ctsi.ntedu.domain.NteduTelExample;

import com.ctsi.ssdc.model.PageResult;
import com.ctsi.ssdc.util.HeaderUtil;
import com.ctsi.ssdc.util.ResponseUtil;


/**
 * REST controller for managing NteduTel.
 *
 * @author ctsi-biyi-generator
 *
 */
@RestController
@RequestMapping("/api")
public class NteduTelController {

    private final Logger log = LoggerFactory.getLogger(NteduTelController.class);

    private static final String ENTITY_NAME = "nteduTel";

    private final NteduTelService nteduTelService;

    public NteduTelController(NteduTelService nteduTelService) {
        this.nteduTelService = nteduTelService;
    }

    @InitBinder   
    public void initBinder(WebDataBinder binder) {   
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");   
        dateFormat.setLenient(true);   
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));   
    } 
   
    /**
     * POST  /nteduTels : Create a new nteduTel.
     *
     * @param nteduTel the nteduTel to create
     * @return the ResponseEntity with status 201 (Created) and with body the new nteduTel, or with status 400 (Bad Request) if the nteduTel has already an id
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/nteduTels")
    public ResponseEntity<NteduTel> createNteduTel(@RequestBody NteduTel nteduTel) throws URISyntaxException {
        log.debug("REST request to save NteduTel : {}", nteduTel);
        NteduTel result = nteduTelService.insert(nteduTel);
        return ResponseEntity.created(new URI("/api/nteduTels" + "/" +result.getId() ))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }
	
    /**
     * PUT  /nteduTels : Updates an existing nteduTel.
     *
     * @param nteduTel the nteduTel to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated nteduTel,
     * or with status 400 (Bad Request) if the nteduTel is not valid,
     * or with status 500 (Internal Server Error) if the nteduTel couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/nteduTels")
    public ResponseEntity<NteduTel> updateNteduTel(@RequestBody NteduTel nteduTel)  {
        log.debug("REST request to update NteduTel : {}", nteduTel);
        NteduTel result = nteduTelService.update(nteduTel);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }
    
    /**
     * GET  /nteduTels : get the nteduTels with page.
     *
     * @return the ResponseEntity with status 200 (OK) and the list of nteduTels in body
     */
    @GetMapping("/nteduTelsByCriteria")
    public PageResult<NteduTel> getNteduTelsByCriteria(NteduTelExample nteduTelExample, Pageable page) {
        log.debug("REST request to get NteduTelsByCriteria");
        return nteduTelService.findByExample(nteduTelExample, page);
    }
    
    /**
     * GET  /nteduTels : get the nteduTels.
     *
     * @return the ResponseEntity with status 200 (OK) and the list of nteduTels in body
     */
    @GetMapping("/nteduTelsList")
    public PageResult<NteduTel> getNteduTelsList(NteduTelExample nteduTelExample) {
        log.debug("REST request to get NteduTelsList");
        return nteduTelService.findByExample(nteduTelExample);
    }

    /**
     * GET  /nteduTels/:id : get the "id" nteduTel.
     *
     * @param id the id of the nteduTel to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the nteduTel, or with status 404 (Not Found)
     */
    @GetMapping("/nteduTels/{id}")
    public ResponseEntity<NteduTel> getNteduTel(@PathVariable Integer id) {
        log.debug("REST request to get NteduTel : {}", id);
        NteduTel nteduTel = nteduTelService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(nteduTel));
    }
	
    /**
     * DELETE  /nteduTels/:id : delete the "id" nteduTel.
     *
     * @param id the id of the nteduTel to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/nteduTels/{id}")
    public ResponseEntity<Void> deleteNteduTel(@PathVariable Integer id) {
        log.debug("REST request to delete NteduTel : {}", id);
        nteduTelService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
    
}
